package com.wocao.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wocao.entity.ReqBody;
import com.wocao.entity.SysRole;
import com.wocao.mapper.SysDeptMapper;
import com.wocao.mapper.SysRoleMapper;
import com.wocao.service.SysRoleService;
import com.wocao.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 角色信息表 服务实现类
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public Page<SysRole> selectAllPage(Page page, ReqBody reqBody) {
        return sysRoleMapper.selectAllPage(page,reqBody);
    }

    @Override
    public void openStatus(long roleId) {
        sysRoleMapper.openStatus(roleId);
    }

    @Override
    public void closeStatus(long roleId) {
        sysRoleMapper.closeStatus(roleId);
    }

    @Transactional
    @Override
    public void addRole(SysRole sysRole, Object[] menuIds) {
        sysRoleMapper.insert(sysRole);
        if(menuIds.length!=0){
            sysRoleMapper.addRoleMenu(sysRole.getRoleId(),menuIds);
        }
    }

    @Override
    public List<Long> selectMenuIdsByRoleId(long roleId) {
        return sysRoleMapper.selectMenuIdsByRoleId(roleId);
    }

    @Transactional
    @Override
    public void roleEdit(SysRole sysRole, Object[] menuIds) {
        sysRoleMapper.deleteMenuIdsByRoleId(sysRole.getRoleId());
        if (menuIds.length!=0){
            sysRoleMapper.addRoleMenu(sysRole.getRoleId(),menuIds);
        }
        sysRoleMapper.updateById(sysRole);
    }

    @Override
    public void deleteRole(long roleId) {
        sysRoleMapper.deleteRole(roleId);
    }

    @Override
    public void deleteRoles(Object[] roleIds) {
        sysRoleMapper.deleteRoles(roleIds);
    }

    @Transactional
    @Override
    public void editRoleDept(SysRole sysRole, Object[] deptIds) {
        sysDeptMapper.deleteDeptIdsByRoleId(sysRole.getRoleId());
        if (deptIds.length!=0){
            sysDeptMapper.addDeptIdsByRoleId(sysRole.getRoleId(),deptIds);
        }
        sysRoleMapper.editDataScope(sysRole.getDataScope(),sysRole.getRoleId());

    }

    @Override
    public void derive(HttpServletResponse response, String[] roleIds) throws IOException {
//        List<SysRole> sysRoles = sysRoleMapper.selectBatchIds(Collections.singleton(roleIds));
        List<SysRole> sysRoles = new ArrayList<>();
        for (int i = 0; i < roleIds.length; i++) {
            SysRole sysRole = sysRoleMapper.selectById(roleIds[i]);
            sysRoles.add(sysRole);
        }
        ExcelUtil.export(response,SysRole.class,sysRoles);
    }
}
